/******************************************************************
 * Created by: DO HOANG HAI                                       
 * Date of Creation:                                               
 * Date of last modification: 1st/Sep/2007
 *                                                                
 * Description: Provide global static method which performs shared 
 * functions or procedures.
 * 
 * Status: Optimized and finished
 ******************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Text.RegularExpressions;

namespace Pbdms.Common
{
    /// <summary>
    /// a class provides public function which can be accessed by any entity.
    /// </summary>
    public class Global
    {   
        /// <summary>
        /// show message to inform user
        /// </summary>
        /// <param name="strMessage"></param>
        public static void showMsgOneButton(String strMessage, char c)
        {
            (new GUI.MsgBoxOneButton(strMessage,c)).ShowDialog();
        }

        /// <summary>
        /// dialog box with message and 2 options
        /// </summary>
        /// <param name="strMessage"></param>
        /// <param name="style"></param>
        /// <returns></returns>
        public static int showMsgTwoButtons(String strMessage, int style, char c)
        {
            GUI.MsgBoxTwoButtons msgConfirmation = new Common.GUI.MsgBoxTwoButtons(strMessage, style,c);
            msgConfirmation.ShowDialog();
            return msgConfirmation.returnValue;
        }

        /// <summary>
        /// dialog box with message and 3 options
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static int showMsgThreeButtons(String msg, char c)
        {
            GUI.MsgBoxThreeButtons msgConfirmation = new Common.GUI.MsgBoxThreeButtons(msg,c);
            msgConfirmation.ShowDialog();
            return msgConfirmation.returnValue;
        }

        /// <summary>
        /// paint the back ground of all the forms in the application
        /// </summary>
        /// <param name="frm"></param>
        public static void paintBackGround(System.Windows.Forms.Form frm)
        {
            frm.BackColor = Color.FromArgb(201, 213, 228);

            foreach (Control ctr in frm.Controls)
            {
                if (ctr.GetType().Equals(typeof(Label)) || ctr.GetType().Equals(typeof(CheckBox))
                    || ctr.GetType().Equals(typeof(RadioButton)) || ctr.GetType().Equals(typeof(TabPage))
                    || ctr.GetType().Equals(typeof(TabControl)) || ctr.GetType().Equals(typeof(Panel))
                    || ctr.GetType().Equals(typeof(GroupBox)) || ctr.GetType().Equals(typeof(PictureBox))
                    || ctr.GetType().Equals(typeof(StatusStrip)) || ctr.GetType().Equals(typeof(ToolStrip)))
                    ctr.BackColor = Color.FromArgb(201, 213, 228);

            }
            
        }

        /// <summary>
        /// get the index of the item with the text given in the dropdownlist (comboBox)
        /// </summary>
        /// <param name="ddl"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static int getDdlItemByStringValue(ComboBox ddl, string value)
        {
            if (ddl.Items.Count != 0)
            {
                for (int i = 0; i < ddl.Items.Count; i++)
                {
                    if (ddl.Items[i].ToString().ToLower().Equals(value.ToLower()))
                        return i;
                }
            }
            return -1;
        }
        /// <summary>
        /// copy a file locally or downloading from internet
        /// </summary>
        /// <param name="source"></param>
        /// <param name="destination"></param>
        public static void copyFile(string source, string destination)
        {
            try
            {
                if (source.Contains("http"))
                {
                    //file is on local machine
                    //System.IO.File.Copy(source , destination , true);
                }
                else if (isAnIPAddress(source))
                {

                }
                else
                {
                    //file is on local machine   
                    System.IO.File.Copy(source, destination, true);    
                }
            }
            catch(Exception ex)
            {
                showMsgOneButton("Couldn't copy the file!\r\nError: " + ex.Message, 'C');
            }      

        }

        /// <summary>
        /// download a file and save to a local address.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="destination"></param>
        public static void downloadFile(string source, string destination)
        {
            try
            {
                copyFile(source, destination);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// to test whether a string is in the format of an IP V4 address
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public static bool isAnIPAddress(string p)
        {
            //use regular expression to check whether this is an ipaddress

            string ifFormater = "^" +
              @"([01]?\d\d|2[0-4]\d|25[0-5])\." +
              @"([01]?\d\d|2[0-4]\d|25[0-5])\." +
              @"([01]?\d\d|2[0-4]\d|25[0-5])\." +
              @"([01]?\d\d|2[0-4]\d|25[0-5])" +
              "$";
            Match ipMatcher = Regex.Match(p, ifFormater);
            return ipMatcher.Success;
        }
        /// <summary>
        /// get the startup path of the program
        /// </summary>
        /// <returns></returns>
        public static String startupPath()
        {
            return Application.StartupPath;
        }

        /// <summary>
        /// show the form which report the list of errors
        /// </summary>
        /// <param name="arlItems"></param>
        /// <param name="title"></param>
        public static void showErrorReporter(ArrayList arlItems, String title)
        {
            (new GUI.FrmErrorReporter(arlItems, title)).ShowDialog();
        }

        /// <summary>
        /// construct a bibtex for a list of BibTex elements
        /// </summary>
        /// <param name="arlOfComponentBibTexStructs"></param>
        /// <returns></returns>
        public static string constructBibTex(ArrayList arlOfComponentBibTexStructs)
        {
            return (new BibTexConstructor(arlOfComponentBibTexStructs)).getBibTex();

        }
        /// <summary>
        /// get the address of the Library folder
        /// </summary>
        /// <returns></returns>
        public static string getLibFolder()
        {
            return startupPath() + "\\Library";
        }
    }
}
